package com.itqf.controller;

import com.google.gson.Gson;
import com.itqf.entity.*;
import com.itqf.service.AddressService;
import com.itqf.service.CartService;
import com.itqf.service.OrderService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.service.impl.CartServiceImpl;
import com.itqf.service.impl.OrderServiceImpl;
import com.itqf.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "OrderController", value = "/order")


public class OrderController extends BaseServlet {

    //初始化业务逻辑对象
    private static AddressService addressService = new AddressServiceImpl();
    private static CartService cartService = new CartServiceImpl();
    private static OrderService orderService = new OrderServiceImpl();


    /**
     * 展示订单信息
     *
     * @param request
     * @param response
     * @return
     */
    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //1.获取参数——uid
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();
        //2.调用业务逻辑，根据uid查询购物车、地址信息
        List<Address> addressList = addressService.findAddressByUid(uid);
        List<Cart> cartList = cartService.findAll(uid);
        //3.将获取到的信息存入共享域中
        request.getSession().setAttribute("addressList", addressList);
        request.getSession().setAttribute("cartList", cartList);
        //跳转至订单信息页面
        return Constants.FORWARD + "/order.jsp";
    }

    public String create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取请求参数
        String aid = request.getParameter("aid");
        String uid = request.getParameter("uid");
        String sum = request.getParameter("sum");
        //2.调用业务逻辑生成订单
        orderService.createOrder(aid, uid, sum);
        //返回响应
        return "订单生成成功！！！";
    }


    public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("loginUser");

        if (user == null) {
            session.setAttribute("msg", "登录后可以查看订单！");
            return Constants.REDIRECT + "/login.jsp";
        }

        List<Orders> ordersList = orderService.findOrdersByUid(user.getUid());
        request.setAttribute("ordersList", ordersList);
        return Constants.FORWARD  + "/orderList.jsp";
    }


    public String detail(HttpServletRequest request , HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取订单的ID
        String oid = request.getParameter("oid");

        //2.调用业务逻辑，根据oid查询订单信息
        Orders order = orderService.findOrdersByOid(oid);

        //3.将订单信息存入共享域中
        request.getSession().setAttribute("order",order);


        System.out.println(order.getAddress());
        return Constants.FORWARD+"orderDetail.jsp" ;
    }

    public String success(HttpServletRequest request , HttpServletResponse response) throws SQLException {
        //1.获取请求参数（oid，result）
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");

        //将获取等的支付状态信息映射到实体类中
        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);

        //获取支付结果
        String result_code = weiXin.getResult().getResult_code();

        if (result_code != null && result_code.equals("SUCCESS")) {
            //支付成功
            //修改订单的支付状态
            //返回订单列表页面
            orderService.updateStateByOid(oid);

            return Constants.FORWARD+"/order?method=show";
        }else{
            //支付失败
            HttpSession session = request.getSession();
            session.setAttribute("msg","订单:"+oid+"支付失败！！！");
            //返回message.jsp页面
            return Constants.REDIRECT+"/message.jsp";
        }
    }

}
